import 'package:flutter/material.dart';
import 'package:txtest/cache.dart';

class LoginPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return LoginPageState();
  }

}

class LoginPageState extends State{
  final TextEditingController _userNameController = new TextEditingController(text: '');
  final TextEditingController _passWordController = new TextEditingController(text: '');
  final GlobalKey _formKey = GlobalKey<FormState>();

  login(BuildContext _context){
    String userName = _userNameController.text;
    String passWord = _passWordController.text;
    if(userName == "zhaoyy" && passWord == "123456"){
      setUserToken("111222");
      goHomePage(_context);
    }
    else{
      showDialog(
        context: _context,
        builder: (_context) {
          return AlertDialog(
            title: new Text('错误'),
            content: new Text('用户名密码错误'),
          );
        },
      );
    }
  }

  goHomePage(BuildContext _context){
    Navigator.pushNamedAndRemoveUntil(_context, "/",(Route<dynamic> route) => false);
  }

  Widget buildLoginForm(BuildContext _context){
    return Form(
      key: _formKey,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            'images/tx_logo.png',
            width: 400,
          ),
          TextFormField(
            controller: _userNameController,
            decoration: const InputDecoration(
              icon: Icon(Icons.person),
              hintText: '用户名、手机号、邮箱',
              labelText: '用户名'
            ),
            validator: (String value){
              return value.isEmpty ? '用户名不能为空' : null;
            },
          ),
          TextFormField(
            controller: _passWordController,
            decoration: const InputDecoration(
              icon: Icon(Icons.vpn_key),
              hintText: '密码、验证码',
              labelText: '密码'
            ),
            obscureText: true,
            validator: (String value){
              return value.isEmpty ? '密码不能为空' : null;
            },
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16.0),
            child: RaisedButton(
              onPressed: (){
                // if(!_formKey.currentState.validate()){
                //   return;
                // }
                login(_context);
              },
              child: Text('登录'),
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext _context) {
    // TODO: implement build
    return new Scaffold(
      // appBar: new AppBar(
      //   title: new Text("登录"),
      // ),
      body: new Padding(
        padding: EdgeInsets.only(
          left: 32.0,
          right: 32.0
        ),
        child: new Center(
          child: buildLoginForm(_context)
        ),
      )
      
    );
  }

}